package js.compont.ueditor.jsp.src.com.baidu.ueditor.action;

import com.baidu.ueditor.ActionEnter;
import com.baidu.ueditor.PathFormat;
import com.baidu.ueditor.define.AppInfo;
import com.baidu.ueditor.define.BaseState;
import com.baidu.ueditor.define.State;
import com.baidu.ueditor.upload.StorageManager;
import com.cernet.abroad.action.SuperAdminManageBaseAction;
import com.cysyz.mylib.io.dirgenerator.impl.DateStyleDirGenerator;
import com.cysyz.mylib.io.filenamegenerator.impl.FileNameGeneratorTimeStamp;
import org.json.JSONException;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created by abroad on 2014/9/5.
 */
public class UeditorAction extends SuperAdminManageBaseAction {

    private File upfile;

    // 封装上传文件类型的属性
    private String upfileContentType;
    // 封装上传文件名的属性
    private String upfileFileName;

    private String action;
    public String fileUpload() throws IOException, JSONException {
        getRequest().setCharacterEncoding( "utf-8" );
        getResponse().setHeader("Content-Type", "text/html");

        String rootPath = getRealPath("/");

        if (action != null && action.equals("config")) {
            getWriter().write(new ActionEnter(getRequest(), rootPath).exec());
        } else if (action != null && action.equals("uploadimage")){
            State state = saveFile(new FileInputStream(upfile));
            getWriter().write(state.toJSONString());
        }

        return NONE;
    }

    public State saveFile(InputStream is) {
        try {
            String savePath = new DateStyleDirGenerator().generate("upload", null) + "/" + new FileNameGeneratorTimeStamp().generate(upfileFileName);
            State storageState = StorageManager.saveFileByInputStream(is,
                    "/home/abroad/content/html/" + savePath, 2048000);
            is.close();

            if (storageState.isSuccess()) {
                storageState.putInfo("url", PathFormat.format(savePath));
                storageState.putInfo("type", upfileContentType);
                storageState.putInfo("original", upfileFileName);
            }

            return storageState;
        } catch (IOException e) {
            return new BaseState(false, AppInfo.IO_ERROR);
        }
        //return new BaseState(false, AppInfo.IO_ERROR);
    }


    public File getUpfile() {
        return upfile;
    }

    public void setUpfile(File upfile) {
        this.upfile = upfile;
    }

    public String getUpfileContentType() {
        return upfileContentType;
    }

    public void setUpfileContentType(String upfileContentType) {
        this.upfileContentType = upfileContentType;
    }

    public String getUpfileFileName() {
        return upfileFileName;
    }

    public void setUpfileFileName(String upfileFileName) {
        this.upfileFileName = upfileFileName;
    }

    public String getAction() {
        return action;
    }

    public void setAction(String action) {
        this.action = action;
    }
}
